/*
* NumberTile
*
* Copyright (C) Connect Link 2013-
*/


package com.connectlinkcorp.common.ui.numbertiles;

import java.util.ArrayList;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;



public class NumberTile extends LinearLayout {

	private GridView gridView;
	private int numberOfRows;
	private int numberOfColumns;
	private int positionX;
	private int positionY; 
	private int width; 
	private int height;
	private int tileSize;
	ArrayList<Integer> numbers;
	private int [] imageDigit;
	private CustomAdapter adapter;
	private Context context;
	private int rowLayoutId;
	
	public NumberTile(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		numbers = new ArrayList<Integer>();
		this.context = context;
	}
	
	public NumberTile (Context context, AttributeSet  attr) {
		super(context, attr);
		// TODO Auto-generated constructor stub
		this.context= context; 
	}
	
	
	public NumberTile (int mRow, int mColumn, int X, int Y, int mWidth, int mHeight, ArrayList<Integer> numberList, Context context){
		super(context);
		this.context = context;
		numberOfRows = mRow;
		numberOfColumns = mColumn;
		positionX = X;
		positionY = Y; 
		width = mWidth; 
		height = mHeight;
		numbers = new ArrayList<Integer>(numberList);
		tileSize = (int) (width/numberOfColumns);
	}
	
	public void setUpParam (int mRow, int mColumn, int X, int Y, int mWidth, int mHeight, ArrayList<Integer> numberList, Context context) {
		this.context = context;
		numberOfRows = mRow;
		numberOfColumns = mColumn;
		positionX = X;
		positionY = Y; 
		width = mWidth; 
		height = mHeight;
		numbers = new ArrayList<Integer>(numberList);
		tileSize = (int) (width/numberOfColumns);
	}
	
	public void show () {
		gridView = new GridView(context);
		this.addView(gridView);
		LayoutParams param = new LayoutParams(width, height);
		param.setMargins(positionX, positionY, 0, 0);
		gridView.setLayoutParams(param);
		gridView.setNumColumns(numberOfColumns);
		
		//creat and add number tiles
		adapter = new CustomAdapter(numbers, tileSize, context, rowLayoutId);
		gridView.setAdapter(adapter);
	}
	
	

	public int[] getImageDigit() {
		return imageDigit;
	}

	public void setImageDigit(int[] imageDigit) {
		this.imageDigit = imageDigit;
	}

	public int getRowLayoutId() {
		return rowLayoutId;
	}

	public void setRowLayoutId(int rowLayoutId) {
		this.rowLayoutId = rowLayoutId;
	}

	public GridView getGridView() {
		return gridView;
	}

	public void setGridView(GridView gridView) {
		this.gridView = gridView;
	}

	public int getNumberOfRows() {
		return numberOfRows;
	}

	public void setNumberOfRows(int numberOfRows) {
		this.numberOfRows = numberOfRows;
	}

	public int getNumberOfColumns() {
		return numberOfColumns;
	}

	public void setNumberOfColumns(int numberOfColumns) {
		this.numberOfColumns = numberOfColumns;
	}

	public int getPositionX() {
		return positionX;
	}

	public void setPositionX(int positionX) {
		this.positionX = positionX;
	}

	public int getPositionY() {
		return positionY;
	}

	public void setPositionY(int positionY) {
		this.positionY = positionY;
	}

	public int getWidthOfView() {
		return width;
	}

	public void setWidthOfView(int width) {
		this.width = width;
	}

	public int getHeightOfView() {
		return height;
	}

	public void setHeightOfView(int height) {
		this.height = height;
	}

	public ArrayList<Integer> getNumbers() {
		return numbers;
	}

	public void setNumbers(ArrayList<Integer> numbers) {
		this.numbers = numbers;
	}

	public int getTileSize() {
		return tileSize;
	}

	public void setTileSize(int tileSize) {
		this.tileSize = tileSize;
	}

	public CustomAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(CustomAdapter adapter) {
		this.adapter = adapter;
	}
	
	public void setOnItemClicked (OnItemClickListener onClick) {
		gridView.setOnItemClickListener(onClick);
	}
	
	// adapter to view tile
	protected class CustomAdapter extends BaseAdapter {

			Context mContenxt;
			ArrayList<Integer> mData;
			int mTileSize;
			int rowLayoutId;
			
			public CustomAdapter (ArrayList<Integer> data, int sizeOfTile, Context context, int rowLayoutId) {
				mContenxt = context;
				mData = data;
				mTileSize = sizeOfTile;
				this.rowLayoutId = rowLayoutId;
			}
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return mData.size();
			}

			@Override
			public Object getItem(int position) { 
				// TODO Auto-generated method stub
				return mData.get(position);
			}

			@Override
			public long getItemId(int id) {
				// TODO Auto-generated method stub
				return id;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				LayoutInflater inflater = (LayoutInflater) mContenxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				View rowview = inflater.inflate(rowLayoutId, parent, false);

				android.widget.AbsListView.LayoutParams param = new android.widget.AbsListView.LayoutParams(mTileSize, mTileSize);
				rowview.setLayoutParams(param);
				if (imageDigit != null && imageDigit.length  >= mData.get(position)){
					rowview.setBackgroundResource(imageDigit[mData.get(position)]);
				}
				return rowview;
			}
			
		}
	
}
